package kuangsheng;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

public class TcpClientDemo02 {
    public static void main(String[] args) throws IOException {
        Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 9999);
        OutputStream outputStream = socket.getOutputStream();
        //文件流
        //读取文件
        FileInputStream fileInputStream = new FileInputStream(new File("E:\\IdeaProjects\\java_practice\\NetworkTest\\test.png"));
        //写出文件
        byte[] buffer = new byte[1024];
        int len;
        while ((len=fileInputStream.read(buffer))!=-1){
            outputStream.write(buffer,0,len);
        }

        //通知服务器我已经传输完了
        socket.shutdownOutput();

        //确定服务器接收完毕才能端口
        InputStream inputStream = socket.getInputStream();
        ByteArrayOutputStream outputStream1 = new ByteArrayOutputStream();
        byte[] buffer1 = new byte[1024];
        int len1;
        while ((len1=inputStream.read(buffer1))!=-1){
            outputStream1.write(buffer1,0,len1);
        }
        System.out.println(outputStream1.toString());
        //关闭资源
        outputStream1.close();
        fileInputStream.close();
        outputStream.close();
        socket.close();

    }
}
